package cn.thoughtworks.school.programCenter.controllers;

import cn.thoughtworks.school.programCenter.annotations.Auth;
import cn.thoughtworks.school.programCenter.entities.QuizNote;
import cn.thoughtworks.school.programCenter.entities.User;
import cn.thoughtworks.school.programCenter.exceptions.BusinessException;
import cn.thoughtworks.school.programCenter.services.QuizNoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/api/v2")

public class QuizNoteController {
    @Autowired
    private QuizNoteService quizNoteService;

    @PostMapping(value = "/quizNote")
    public ResponseEntity addNoteToQuiz(@RequestBody QuizNote quizNote) {
        quizNoteService.addNoteToQuiz(quizNote);
        return new ResponseEntity<>(quizNote, HttpStatus.CREATED);
    }

    @GetMapping(value = "/myNotes/{programId}")
    public ResponseEntity getMyNotes(@Auth User current, @PathVariable Long programId) {
        List<Map> quizNoteList = quizNoteService.getMyNotes(current.getId(), programId);
        return new ResponseEntity<>(quizNoteList, HttpStatus.CREATED);
    }

    @DeleteMapping(value = "/myNotes/{noteId}")
    public ResponseEntity deleteNoteById(@PathVariable Long noteId) throws BusinessException {
        quizNoteService.deleteNoteById(noteId);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

}
